package time

import (
	"time"
)

const (
	// Monday 周一
	Monday int64 = iota + 1
	// Tuesday 周二
	Tuesday
	// Wednesday 周三
	Wednesday
	// Thursday 周四
	Thursday
	// Friday 周五
	Friday
	// Saturday 周六
	Saturday
	// Sunday 周日
	Sunday
)

/*DayInWeek 计算给定的时间在本周是第几天*/
func DayInWeek(day time.Time) int64 {
	dayName := day.Weekday().String()
	switch dayName {
	case "Sunday":
		return Sunday
	case "Monday":
		return Monday
	case "Tuesday":
		return Tuesday
	case "Wednesday":
		return Wednesday
	case "Thursday":
		return Thursday
	case "Friday":
		return Friday
	case "Saturday":
		return Saturday
	default:
		return 0
	}
}

// LastWeekStartAndEnd 获取上周的起始时间和结束时间
func LastWeekStartAndEnd(now time.Time) (time.Time, time.Time) {
	weekDay := DayInWeek(now)
	// 获取今天的0点时间
	zero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
	// 获取上周起始时间
	wstart := zero.AddDate(0, 0, int(-(weekDay + 6)))
	wend := wstart.AddDate(0, 0, 6).Add(time.Hour * 23).Add(time.Minute * 59).Add(time.Second * 59)
	return wstart, wend
}

// ThreeWeekDayBeforeNow 计算今天往前三个工作日的开始和结束时间
func CalWeekDayBeforeNow(now time.Time, beforeDay int) []time.Time {
	if beforeDay > 5 {
		beforeDay = 5
	}
	var lastDay time.Time
	var timeArr []time.Time
	// 获取今天的0点时间
	zero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
	lastDay = zero
	// 获取上周起始时间
	for i := 0; i < beforeDay+2; i++ {
		lastDay = lastDay.AddDate(0, 0, -1)
		dayInWeek := DayInWeek(lastDay)
		// 如果是周六周日跳过
		if dayInWeek == 6 || dayInWeek == 7 {
			continue
		}
		// 放入到数组中
		timeArr = append(timeArr, lastDay)
		if len(timeArr) == beforeDay {
			break
		}
	}
	// 取最小时间和最大时间返回
	return timeArr
}
